package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.Car;
import com.supinfo.suptracking.entity.User;
import com.supinfo.suptracking.entity.Zone;
import com.supinfo.suptracking.service.CarService;
import com.supinfo.suptracking.service.ZoneService;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "ListCar", urlPatterns = {"/auth/listCar"})
public class ListCarServlet extends HttpServlet {

    @EJB
    private CarService carServ;
    
    @EJB
    private ZoneService zoneServ;
        

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        User userLogged =(User)request.getSession().getAttribute("userlogged");
        List<Car> listCars = carServ.processGetAllCarByUser(userLogged);
        List<Zone> listZones = zoneServ.processGetAllZone();
        
        if(listCars.isEmpty()){
            System.out.println("Je suis vide");
            response.sendRedirect(getServletContext().getContextPath()+ "/jsp/auth/listCars.jsp");
        }
        else{  
        System.out.println("liste recuperee");
        request.getSession().setAttribute("listZoneslistCars", listZones);
        request.getSession().setAttribute("listCars", listCars);
        response.sendRedirect(getServletContext().getContextPath()+ "/jsp/auth/listCars.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);  
    }
}
